using AutoMapper;
using SqlSugar;
using SX.BaseService.Repository.Core;
using SX.BaseService.Repository.Entity;
using SX.BaseService.Service.SysDictionary.ViewModel;
using SX.BaseService.Service.ViewModel;
using SX.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SX.BaseService.Service
{
    /// <summary>
	/// 字典管理服务层
	/// </summary>
    public class SysDictionaryService:DbContext,ISysDictionaryService
    {
        private readonly IRepository<SysDictionaryEntity> _repository;
        private readonly IMapper _mapper;
        public SysDictionaryService(IRepository<SysDictionaryEntity> repository, IMapper mapper)
        {
            _mapper = mapper;
            _repository = repository;
        }

        public async Task<IApiResult> Query(string name)
        {
            var list = await Db.Queryable<SysDictionaryEntity>().WhereIF(name.NotNull(), x => x.Name.Contains(name)).OrderBy(x=>x.Sort).ToListAsync();
            return ApiResult.Ok(list);
        }

        public async Task<IApiResult> QueryParents()
        {
            var list =await Db.Queryable<SysDictionaryEntity>().Where(x => x.ParentId == null).Select(x => new { x.Id, x.Name }).ToListAsync();
            return ApiResult.Ok(list);
        }

        public async Task<IApiResult> Add(DictionaryAddModel model)
        {
            var entity = _mapper.Map<SysDictionaryEntity>(model);
            entity.Id = GuidHelper.NewSequentialGuid(SequentialGuidType.SequentialAtEnd);
            entity.CreateTime = DateTime.Now;

            var res = await _repository.Add(entity);
            if (res>0)
            {
                return ApiResult.Ok();
            }
            else
            {
                return ApiResult.NotOk("添加字典失败");
            }

        }

        public async Task<IApiResult> Delete(List<string> ids)
        {
            var res = await _repository.Delete(ids);
            if (res > 0)
                return ApiResult.Ok(res, "成功删除" + res + "条数据");
            else
                return ApiResult.NotOk("删除失败");
        }

        public async Task<IApiResult> GetById(string id)
        {
            if (id.IsNull()) return ApiResult.NotOk("参数【id】错误");
            var res = await _repository.GetId(id);
            res.Parent = new List<SysDictionaryEntity>();
            if (res.ParentId.NotNull() && res.ParentId != "0")
            {
                var parent = await _repository.GetId(res.ParentId);
                parent.Selected = true;
                res.Parent.Add(parent);

            }
            //后续修改
            var res2 = await _repository.GetAll();
            var list = res2.Where(a => a.ParentId == null && a.Id != res.ParentId);
            res.Parent.AddRange(list);
            return ApiResult.Ok(res);
        }

        public async Task<IApiResult> Update(DictionaryUpdateModel model)
        {
            var entity = _mapper.Map<SysDictionaryEntity>(model);
            var res = await _repository.Update(entity);
            if (res>0)
                return ApiResult.Ok("更新成功");
            else
                return ApiResult.NotOk("更新失败");
        }
    }
}